#include "hikari.h"

HikariEventReceiver::HikariEventReceiver(void){
	for (u32 i=0; i<KEY_KEY_CODES_COUNT; i++){
		KeyIsDown[i] = false;
		KeyIsPressed[i]=false;
		KeyIsReleased[i]=false;
	}

	lMouseButtonState=false;
	rMouseButtonState=false;
	mMouseButtonState=false;

	lMousePressed=false;
	rMousePressed=false;
	mMousePressed=false;

	mouseWheel=0;

	mouseX=0;
	mouseY=0;

	mouseSpeedX=0;
	mouseSpeedY=0;

	lastMouseX=0;
	lastMouseY=0;

	enabled=true;
}

HikariEventReceiver::~HikariEventReceiver(void){}

void HikariEventReceiver::processMouseSpeed(){
	mouseSpeedX=mouseX-lastMouseX;
	mouseSpeedY=mouseY-lastMouseY;
	lastMouseX=mouseX;
	lastMouseY=mouseY;
}

bool HikariEventReceiver::OnEvent(const SEvent& event){
	if(!enabled)return false;
	if (event.EventType == irr::EET_KEY_INPUT_EVENT){
		bool value=event.KeyInput.PressedDown;
		
		if(value==true){
			if(!IsKeyDown(event.KeyInput.Key)){
				KeyIsPressed[event.KeyInput.Key]=true;
			}
		}
		if(value==false){
			KeyIsReleased[event.KeyInput.Key]=true;
		}
		KeyIsDown[event.KeyInput.Key] = value;
	}else if(event.EventType == irr::EET_MOUSE_INPUT_EVENT){
		switch(event.MouseInput.Event){
		case irr::EMIE_LMOUSE_PRESSED_DOWN:
			lMouseButtonState=true;
			lMousePressed=true;
			break;

		case irr::EMIE_LMOUSE_LEFT_UP:
			lMouseButtonState=false;
			break;

		case irr::EMIE_RMOUSE_PRESSED_DOWN:
			rMouseButtonState=true;
			rMousePressed=true;
			break;

		case irr::EMIE_RMOUSE_LEFT_UP:
			rMouseButtonState=false;
			break;

		case irr::EMIE_MMOUSE_PRESSED_DOWN:
			mMouseButtonState=true;
			mMousePressed=true;
			break;

		case irr::EMIE_MMOUSE_LEFT_UP:
			mMouseButtonState=false;
			break;

		case irr::EMIE_MOUSE_MOVED:
			mouseX=event.MouseInput.X;
			mouseY=event.MouseInput.Y;
			break;

		case irr::EMIE_MOUSE_WHEEL:
			mouseWheel=event.MouseInput.Wheel;
			break;
		}
	}

	return false;
}
